package goodbaby.dklt.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.core.utils.StringUtil;
import com.squareup.picasso.Picasso;
import com.tgd.gbb.uikit.ui.widget.MyListView;
import com.tgd.gbb.uikit.ui.widget.NoScrollGridView;
import com.tgd.gbb.uikit.ui.widget.asyncimageview.CircleImageView;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.GbbPreferences;
import goodbaby.dklt.R;
import goodbaby.dklt.activity.SurfaceActivity;
import goodbaby.dklt.bean.ImageBean;
import goodbaby.dklt.bean.ParentsCricleBean;
import goodbaby.dklt.bean.ReviewBean;
import goodbaby.dklt.customerview.AtlasSeeActivity;
import goodbaby.dklt.emotion.StringUtils;
import goodbaby.dklt.fragment.HomeFragment;

/**
 * Created by Jayden on 2016/4/25 10:55.
 *
 * @version V1.0
 * @Title: ParentsCircleAdapter
 * @Description: 家校互通适配器
 */
public class ParentsCircleAdapter extends BaseAdapter {

    private Context context;
    private List<ParentsCricleBean> list;
    private ICallBack mCallback;

    public static abstract interface ICallBack {
        public abstract void AddComment(int paramInt1, int paramInt2);

        public abstract void deleMsg(int paramInt1, int paramInt2);

        public abstract void praise(int paramInt1, int paramInt2);

        public abstract void redirect(int paramInt1, int paramInt2);

        public abstract void gag(int position, int id, int userId, String type);
    }

    public ParentsCircleAdapter(Context paramContext, List<ParentsCricleBean> paramList, HomeFragment.MyCallBack paramMyCallBack) {
        this.context = paramContext;
        this.list = paramList;
        this.mCallback = paramMyCallBack;
    }

//    private Bitmap createVideoThumbnail(String paramString) {
//        MediaMetadataRetriever localMediaMetadataRetriever = new MediaMetadataRetriever();
//        try {
//            if (Build.VERSION.SDK_INT >= 14)
//                localMediaMetadataRetriever.setDataSource(paramString, new HashMap());
//            while (true) {
//                Bitmap localBitmap = localMediaMetadataRetriever.getFrameAtTime(1000L);
//                return localBitmap;
//                localMediaMetadataRetriever.setDataSource(paramString);
//            }
//        } catch (IllegalArgumentException localIllegalArgumentException) {
//            return null;
//        } catch (RuntimeException localRuntimeException) {
//            return null;
//        } finally {
//            localMediaMetadataRetriever.release();
//        }
//    }

    public int getCount() {
        return this.list.size();
    }

    public Object getItem(int paramInt) {
        return this.list.get(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    public View getView(final int paramInt, View paramView, ViewGroup paramViewGroup) {
        final PcViewHolder localPcViewHolder;
        if (paramView == null) {
            paramView = LayoutInflater.from(context).inflate(R.layout.item_parents_circle, null);
            localPcViewHolder = new PcViewHolder();
            localPcViewHolder.ivHeadImg = (CircleImageView) paramView.findViewById(R.id.item_circle_headimg);
            localPcViewHolder.tvName = (TextView) paramView.findViewById(R.id.item_tv_name);
            localPcViewHolder.tvTime = (TextView) paramView.findViewById(R.id.item_tv_time);
            localPcViewHolder.tvContent = (TextView) paramView.findViewById(R.id.item_tv_content);
            localPcViewHolder.imgGv = (NoScrollGridView) paramView.findViewById(R.id.gridview);
            localPcViewHolder.tvDel = (TextView) paramView.findViewById(R.id.item_tv_del);
            localPcViewHolder.tvGag = (TextView) paramView.findViewById(R.id.item_tv_gag);
            localPcViewHolder.ivPraise = (ImageView) paramView.findViewById(R.id.item_iv_praise);
            localPcViewHolder.ivComment = (ImageView) paramView.findViewById(R.id.item_iv_comment);
            localPcViewHolder.tvPraiseDetail = (TextView) paramView.findViewById(R.id.item_tv_praise_detail);
            localPcViewHolder.lvComment = (MyListView) paramView.findViewById(R.id.item_commentList);
            localPcViewHolder.ivVedioImage = (ImageView) paramView.findViewById(R.id.item_iv_vedioimage);
            localPcViewHolder.rlVedioImg = (RelativeLayout) paramView.findViewById(R.id.rl_vedio_img);
            paramView.setTag(localPcViewHolder);
        } else {
            localPcViewHolder = (PcViewHolder) paramView.getTag();
        }
        if (!StringUtil.isEmpty(list.get(paramInt).getAnchorimge())) {
            Picasso.with(context).load(list.get(paramInt).getAnchorimge()).fit().into(localPcViewHolder.ivHeadImg);
        } else if (!StringUtil.isEmpty(list.get(paramInt).getTeacherimg())) {
            Picasso.with(context).load(list.get(paramInt).getTeacherimg()).fit().into(localPcViewHolder.ivHeadImg);
        } else {
            Picasso.with(context).load(R.mipmap.ic_launcher).into(localPcViewHolder.ivHeadImg);
        }
        if (!StringUtil.isEmpty((list.get(paramInt)).getChildrenName())) {
            localPcViewHolder.tvName.setText(list.get(paramInt).getChildrenName());
        } else {
            localPcViewHolder.tvName.setText(list.get(paramInt).getTeacherName());
        }

        localPcViewHolder.tvTime.setText(list.get(paramInt).getSendTime());
        localPcViewHolder.tvContent.setText(StringUtils.getEmotionContent(context, localPcViewHolder.tvContent, list.get(paramInt).getDetail()));
//        localPcViewHolder.tvContent.setText(list.get(paramInt).getDetail());
        if (list.get(paramInt).getIs_my() != 0 || Integer.parseInt(GbbPreferences.getRole()) == 2) {
            localPcViewHolder.tvDel.setVisibility(View.VISIBLE);
        } else {
            localPcViewHolder.tvDel.setVisibility(View.GONE);
        }
        if (Integer.parseInt(GbbPreferences.getRole()) == 2 && list.get(paramInt).getROLEID() == 0) {
            localPcViewHolder.tvGag.setVisibility(View.VISIBLE);
        } else {
            localPcViewHolder.tvGag.setVisibility(View.GONE);
        }
//        if (Integer.parseInt(GbbPreferences.getRole()) == 2) {
//            localPcViewHolder.tvDel.setVisibility(View.VISIBLE);
//        } else {
//            localPcViewHolder.tvDel.setVisibility(View.GONE);
//        }
//        if (list.get(paramInt).getROLEID() == 2 || list.get(paramInt).getROLEID() == 3) {
//            localPcViewHolder.tvGag.setVisibility(View.GONE);
//        } else {
//            localPcViewHolder.tvGag.setVisibility(View.VISIBLE);
//        }

        if (list.get(paramInt).getShieId().equals("Y")) {
            localPcViewHolder.tvGag.setText("解禁");
        } else {
            localPcViewHolder.tvGag.setText("禁言");
        }
        localPcViewHolder.tvGag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userId = StringUtil.isEmpty(list.get(paramInt).getAdmin_id()) ? list.get(paramInt).getUser_id() : list.get(paramInt).getAdmin_id();
                if (list.get(paramInt).getShieId().equals("Y")) {
                    mCallback.gag(paramInt, list.get(paramInt).getId(), Integer.parseInt(userId), "N");
                } else {
                    mCallback.gag(paramInt, list.get(paramInt).getId(), Integer.parseInt(userId), "Y");
                }
            }
        });

        localPcViewHolder.tvDel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                mCallback.deleMsg(paramInt, list.get(paramInt).getId());
            }
        });
        localPcViewHolder.ivPraise.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                mCallback.praise(paramInt, list.get(paramInt).getId());
            }
        });
        localPcViewHolder.ivComment.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                mCallback.AddComment(paramInt, list.get(paramInt).getId());
            }
        });

        if (list.get(paramInt).getPraiselist().size() > 2) {
            String praiseName1 = list.get(paramInt).getPraiselist().get(0).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(0).getTeacherName() : list.get(paramInt).getPraiselist().get(0).getChildrenName();
            String praiseName2 = list.get(paramInt).getPraiselist().get(1).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(1).getTeacherName() : list.get(paramInt).getPraiselist().get(1).getChildrenName();
            String praiseName3 = list.get(paramInt).getPraiselist().get(2).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(2).getTeacherName() : list.get(paramInt).getPraiselist().get(2).getChildrenName();
            localPcViewHolder.tvPraiseDetail.setText(praiseName1 + "," + praiseName2 + "," + praiseName3 + "等" + list.get(paramInt).getPraiselist().size() + "人觉得很赞");
            localPcViewHolder.tvPraiseDetail.setVisibility(View.VISIBLE);
        } else if (list.get(paramInt).getPraiselist().size() == 1) {
            String praiseName = list.get(paramInt).getPraiselist().get(0).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(0).getTeacherName() : list.get(paramInt).getPraiselist().get(0).getChildrenName();
            localPcViewHolder.tvPraiseDetail.setText(praiseName + "觉得很赞");
            localPcViewHolder.tvPraiseDetail.setVisibility(View.VISIBLE);
        } else if (list.get(paramInt).getPraiselist().size() == 2) {
            String praiseName1 = list.get(paramInt).getPraiselist().get(0).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(0).getTeacherName() : list.get(paramInt).getPraiselist().get(0).getChildrenName();
            String praiseName2 = list.get(paramInt).getPraiselist().get(1).getChildrenName() == null ? list.get(paramInt).getPraiselist().get(1).getTeacherName() : list.get(paramInt).getPraiselist().get(1).getChildrenName();
            localPcViewHolder.tvPraiseDetail.setText(praiseName1 + "," + praiseName2 + "觉得很赞");
            localPcViewHolder.tvPraiseDetail.setVisibility(View.VISIBLE);
        } else {
            localPcViewHolder.tvPraiseDetail.setVisibility(View.GONE);
        }


        if (list.get(paramInt).getImagelist().size() == 0) {
            localPcViewHolder.imgGv.setVisibility(View.GONE);
        } else {
            localPcViewHolder.imgGv.setVisibility(View.VISIBLE);
            localPcViewHolder.ivVedioImage.setVisibility(View.VISIBLE);
        }

        GridViewAdapter gvAdapter = new GridViewAdapter(list.get(paramInt).getImagelist());
        localPcViewHolder.imgGv.setAdapter(gvAdapter);

        if (list.get(paramInt).getReviewlist().size() > 0) {
            localPcViewHolder.lvComment.setVisibility(View.VISIBLE);
        } else {
            localPcViewHolder.lvComment.setVisibility(View.GONE);
        }
        CommentAdatpter commentAdatpter = new CommentAdatpter(context, list.get(paramInt).getReviewlist());
        localPcViewHolder.lvComment.setAdapter(commentAdatpter);

        if (list.get(paramInt).getType() == 1) {
            localPcViewHolder.imgGv.setVisibility(View.VISIBLE);
            localPcViewHolder.rlVedioImg.setVisibility(View.GONE);
        } else if (list.get(paramInt).getType() == 2) {
            localPcViewHolder.imgGv.setVisibility(View.GONE);
            localPcViewHolder.rlVedioImg.setVisibility(View.VISIBLE);
            localPcViewHolder.ivVedioImage.setVisibility(View.VISIBLE);
            if (list.get(paramInt).getImagelist().size() > 1) {
                Picasso.with(context).load(list.get(paramInt).getImagelist().get(1).getURLPath()).fit().into(localPcViewHolder.ivVedioImage);
            }
            localPcViewHolder.ivVedioImage.setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    Intent localIntent = new Intent(context, SurfaceActivity.class);
                    HomeFragment.toBigImage = 1;
                    localIntent.putExtra("type", 1);
                    localIntent.putExtra("vedioUrl", list.get(paramInt).getImagelist().get(0).getURLPath());
                    context.startActivity(localIntent);
                }
            });
        } else {
            localPcViewHolder.imgGv.setVisibility(View.GONE);
            localPcViewHolder.rlVedioImg.setVisibility(View.GONE);
        }
        return paramView;
    }


    class PcViewHolder {
        NoScrollGridView imgGv;
        ImageView ivComment;
        CircleImageView ivHeadImg;
        ImageView ivPraise;
        ImageView ivVedioImage;
        RelativeLayout rlVedioImg;
        MyListView lvComment;
        TextView tvContent;
        TextView tvDel;
        TextView tvGag;
        TextView tvName;
        TextView tvPraiseDetail;
        TextView tvTime;
    }

    private class GridViewAdapter
            extends
            BaseAdapter {
        private List<ImageBean> imaList = new ArrayList<ImageBean>();
        private HolderView holder;

        public GridViewAdapter(List<ImageBean> List) {
            this.imaList = List;
        }

        @Override
        public int getCount() {
            return imaList.size();
        }

        @Override
        public Object getItem(int position) {
            return imaList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View view, ViewGroup arg2) {

            if (view == null) {
                view = View.inflate(context, R.layout.item_circle_grid, null);
                holder = new HolderView();
                holder.iv_item = (ImageView) view.findViewById(R.id.iv_gridview_item);
                view.setTag(holder);
            } else {
                holder = (HolderView) view.getTag();
            }
            Picasso.with(context).load(imaList.get(position).getURLPath()).fit().into(holder.iv_item);
//            ImageLoaderUtil.loadWebUrl(imaList.get(position), holder.iv_item);
            final List<String> imgList = new ArrayList<String>();
            for (ImageBean image : imaList) {
                imgList.add(image.getURLPath());
            }
            holder.iv_item.setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    Intent intent = new Intent(context, AtlasSeeActivity.class);
                    intent.putExtra("fromType", 5);
                    intent.putExtra("Urls", (Serializable) imgList);
                    intent.putExtra("position", position);
                    ParentsCircleAdapter.this.context.startActivity(intent);
                    HomeFragment.toBigImage = 1;
                }
            });
            return view;
        }

        private class HolderView {
            ImageView iv_item;
        }

    }

    class CommentAdatpter extends ArrayAdapter<ReviewBean> {
        public CommentAdatpter(Context context, List<ReviewBean> commentList) {
            super(context, 0, commentList);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_comment, null);
            }
            ReviewBean comment = getItem(position);
            TextView username = (TextView) convertView.findViewById(R.id.username);
            TextView content = (TextView) convertView.findViewById(R.id.comment);

            //username.setText(comment.getText());

            if (comment.getChildrenName() == null) {
                username.setText(comment.getTeacherName() + ":");
            } else {
                username.setText(comment.getChildrenName() + ":");
            }
            content.setText(comment.getText());
            return convertView;
        }
    }
}
